package com.yx.cloud.map.layer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;

import com.aaa.lib.map.MapView;
import com.aaa.lib.map.layer.PathLayer;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.PathPoint;

import java.util.ArrayList;
import java.util.List;

public class YXCustomPathLayer extends PathLayer {

    private Paint pathPaint;
    private float lineWidth = 0.7f;
    private Canvas pathCanvas;
    private Bitmap pathBitmap;
    private int scale;
    private float[] matrixValue = new float[9];
    private Matrix matrix = new Matrix();
    private boolean isFilter = true;
    private DashPathEffect dashPathEffect;
    private CornerPathEffect cornerPathEffect;


    public YXCustomPathLayer(MapView mapView) {
        super(mapView);
        cornerPathEffect = new CornerPathEffect(5);
        dashPathEffect = new DashPathEffect(new float[]{10, 20}, 2);

        pathPaint = new Paint();
        pathPaint.setStyle(Paint.Style.STROKE);
        pathPaint.setPathEffect(cornerPathEffect);
        pathPaint.setAntiAlias(true);
        pathPaint.setColor(Color.WHITE);
        //连接的外边缘以圆弧的方式相交
        pathPaint.setStrokeJoin(Paint.Join.ROUND);
        //线条结束处绘制一个半圆
        pathPaint.setStrokeCap(Paint.Cap.ROUND);

        pathPaint.setStrokeWidth(lineWidth);


        pathCanvas = new Canvas();
    }

    /**
     * 填充图片的方式的path
     */
    public synchronized void setPath(MapInfo mapBean, com.iot.product.sweeper.bean.Path pathBean) {
        if (mapBean.mapType == mapBean.MAP_TYPE_GYRO) {
            lineWidth = 0.2f;
        }else{
            lineWidth = 0.7f;
        }
        pathPaint.setStrokeWidth(lineWidth);


        if (pathBitmap != null && !pathBitmap.isRecycled()) {
            pathBitmap.recycle();
            pathBitmap = null;
        }
        int length = pathBean.destPathPoints.size();
        LogUtils.i("path size : " + length);
        if (length < 2 || mapBean.width == 0 || mapBean.height == 0) {
            return;
        }

        setScale(mapBean.width, mapBean.height, 3072);
        pathPaint.setStrokeWidth(lineWidth * scale);
        pathBitmap = Bitmap.createBitmap(mapBean.width * scale, mapBean.height * scale, Bitmap.Config.ARGB_8888);
        pathCanvas.setBitmap(pathBitmap);
        pathCanvas.drawColor(Color.TRANSPARENT);

        List<PathPoint> pathPoints = filterPath(pathBean.destPathPoints);

        Path tempPath = new Path();

        //第一个点添加到路径
        PathPoint point = pathPoints.get(0);
        tempPath.moveTo(point.x * scale, point.y * scale);
        int lastPointType = point.type;

        float lastX = point.x * scale;
        float lastY = point.y * scale;
        for (int i = 1; i < pathPoints.size(); i++) {
            point = pathPoints.get(i);
            if (lastPointType == point.type) {
                tempPath.lineTo(point.x * scale, point.y * scale);
            } else {
//                LogUtils.i(" path type : " + i + " " + lastPointType);
                //两种方式
                //从这个点之前的点开始变
                drawPathList(tempPath, lastPointType);
                tempPath = new Path();
                tempPath.moveTo(lastX, lastY);
                tempPath.lineTo(point.x * scale, point.y * scale);
                lastPointType = point.type;

/*              从这个点之后开始变
                tempPath.lineTo(point.x * scale, point.y * scale);
                drawPathList(tempPath, lastPointType);

                lastPointType = point.type;
                tempPath = new Path();
                tempPath.moveTo(point.x * scale, point.y * scale);*/
            }

            lastX = point.x * scale;
            lastY = point.y * scale;
        }
        drawPathList(tempPath, lastPointType);

    }

    private void drawPathList(Path path, int type) {
        //TODO 暂时屏蔽 怕出事 等真的有路径点类型后再放开

        if (type == PathPoint.TYPE_CLEAN) {
            pathPaint.setColor(Color.WHITE);
            pathPaint.setPathEffect(cornerPathEffect);
            pathCanvas.drawPath(path, pathPaint);
        } else if (type == PathPoint.TYPE_TRANSITION) {
/*            pathPaint.setColor(Color.YELLOW);
            pathPaint.setPathEffect(cornerPathEffect);
            pathCanvas.drawPath(path, pathPaint);*/
        } else if (type == PathPoint.TYPE_MOP) {
            pathPaint.setColor(Color.WHITE);
            pathPaint.setPathEffect(cornerPathEffect);
            pathCanvas.drawPath(path, pathPaint);
        } else if (type == PathPoint.TYPE_RECHARGE) {
/*            pathPaint.setPathEffect(dashPathEffect);
            pathPaint.setColor(Color.BLUE);
            pathCanvas.drawPath(path, pathPaint);*/
        }
    }

    private void setScale(int mapWidth, int mapHeight, int targetSize) {
        float scaleWidth = (float) targetSize / (float) mapWidth;
        float scaleHeight = (float) targetSize / (float) mapHeight;
        scale = (int) (scaleWidth < scaleHeight ? scaleWidth : scaleHeight);
    }

    @Override
    public synchronized void draw(Canvas canvas) {
        if (!visible) {
            return;
        }
        drawWithBitmap(canvas);
    }

    public void setMatrix() {
        //因为只把图片放大了, 所以绘制时 需要缩小到原尺寸
        matrix.set(mMapView.getTransform());
        matrix.preScale(1f / scale, 1f / scale);
    }


    private void drawWithBitmap(Canvas canvas) {
        if (pathBitmap != null && !pathBitmap.isRecycled()) {
            LogUtils.i("draw path bitmap: ");
            setMatrix();
            canvas.drawBitmap(pathBitmap, matrix, null);
        }
    }

    private List<PathPoint> filterPath(List<PathPoint> pathPoints) {
        if (isFilter) {
            List<PathPoint> newPath = new ArrayList<>();
            PathPoint p0 = pathPoints.get(0);
            newPath.add(p0);

            int len = pathPoints.size();
            int iA = 0;
            int iB = iA + 1;
            int iC = iB + 1;
            double skipDgr = Math.PI / 180 * 172;
            while (iA < len - 1) {
                iB = iA + 1;
                iC = iB + 1;
                while (iB < len - 1
                        && iC < len - 1
                        && (pathPoints.get(iA).type == pathPoints.get(iB).type && pathPoints.get(iB).type == pathPoints.get(iB + 1).type)
                        && angleBetweenThreePoint(pathPoints.get(iA), pathPoints.get(iB), pathPoints.get(iB + 1)) > skipDgr) {
                    iB = iB + 1;
                    iC = iB + 1;
                }
                newPath.add(pathPoints.get(iB));

                iA = iB;
            }
            return newPath;
        } else {
            List<PathPoint> newPath = new ArrayList<>();
            newPath.addAll(pathPoints);
            return newPath;
        }
    }

    // 计算三点之间弧度
    double angleBetweenThreePoint(PathPoint p1, PathPoint p2, PathPoint p3) {
        double aa = Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
        double bb = Math.pow(p3.x - p2.x, 2) + Math.pow(p3.y - p2.y, 2);
        double cc = Math.pow(p3.x - p1.x, 2) + Math.pow(p3.y - p1.y, 2);
        double C = Math.acos((aa + bb - cc) / (2 * Math.sqrt(aa) * Math.sqrt(bb)));
        return C;
    }
}

